﻿using System.Net;
using System.Net.Sockets;

namespace Honeycomb.Transport.Ipv4Synchronous
{
    /// <summary>
    /// INetworkServer implementation.
    /// </summary>
    public sealed class NetworkServer : INetworkServer
    {
        /// <summary>
        /// TCP listener.
        /// </summary>
        private readonly TcpListener tcpListener;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="address">IP address to listen on.</param>
        /// <param name="port">Port to listen on.</param>
        public NetworkServer(IPAddress address, int port)
        {
            tcpListener = new TcpListener(address, port);
            tcpListener.Start();
        }

        /// <summary>
        /// Accept connection.
        /// </summary>
        /// <returns></returns>
        public INetworkPeer AcceptConnection()
        {
            return new NetworkPeer(tcpListener.AcceptTcpClient());
        }

        /// <summary>
        /// Dispose.
        /// </summary>
        public void Dispose()
        {           
            tcpListener.Stop();
        }
    }
}
